home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / RCS / i386-dep.c,v < prev    next >
Encoding:
Text File  |  1990-12-10  |  31.3 KB  |  1,301 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.11.13.17.12.00;  author rab;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* Low level interface to ptrace, for GDB when running on the Intel 386.
  26.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  27.  
  28. This file is part of GDB.
  29.  
  30. GDB is free software; you can redistribute it and/or modify
  31. it under the terms of the GNU General Public License as published by
  32. the Free Software Foundation; either version 1, or (at your option)
  33. any later version.
  34.  
  35. GDB is distributed in the hope that it will be useful,
  36. but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38. GNU General Public License for more details.
  39.  
  40. You should have received a copy of the GNU General Public License
  41. along with GDB; see the file COPYING.  If not, write to
  42. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  43.  
  44. #include <stdio.h>
  45. #include "defs.h"
  46. #include "param.h"
  47. #include "frame.h"
  48. #include "inferior.h"
  49.  
  50. #ifdef USG
  51. #include <sys/types.h>
  52. #endif
  53.  
  54. #include <sys/param.h>
  55. #include <sys/dir.h>
  56. #include <signal.h>
  57. #include <sys/user.h>
  58. #include <sys/ioctl.h>
  59. #include <fcntl.h>
  60.  
  61. #ifdef COFF_ENCAPSULATE
  62. #include "a.out.encap.h"
  63. #else
  64. #include <a.out.h>
  65. #endif
  66.  
  67. #ifndef N_SET_MAGIC
  68. #ifdef COFF_FORMAT
  69. #define N_SET_MAGIC(exec, val) ((exec).magic = (val))
  70. #else
  71. #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
  72. #endif
  73. #endif
  74.  
  75. #include <sys/file.h>
  76. #include <sys/stat.h>
  77.  
  78. #include <sys/reg.h>
  79.  
  80. extern int errno;
  81.  
  82. /* This function simply calls ptrace with the given arguments.  
  83.    It exists so that all calls to ptrace are isolated in this 
  84.    machine-dependent file. */
  85. int
  86. call_ptrace (request, pid, arg3, arg4)
  87.      int request, pid, arg3, arg4;
  88. {
  89.   return ptrace (request, pid, arg3, arg4);
  90. }
  91.  
  92. kill_inferior ()
  93. {
  94.   if (remote_debugging)
  95.     return;
  96.   if (inferior_pid == 0)
  97.     return;
  98.   ptrace (8, inferior_pid, 0, 0);
  99.   wait (0);
  100.   inferior_died ();
  101. }
  102.  
  103. /* This is used when GDB is exiting.  It gives less chance of error.*/
  104.  
  105. kill_inferior_fast ()
  106. {
  107.   if (remote_debugging)
  108.     return;
  109.   if (inferior_pid == 0)
  110.     return;
  111.   ptrace (8, inferior_pid, 0, 0);
  112.   wait (0);
  113. }
  114.  
  115. /* Resume execution of the inferior process.
  116.    If STEP is nonzero, single-step it.
  117.    If SIGNAL is nonzero, give it that signal.  */
  118.  
  119. void
  120. resume (step, signal)
  121.      int step;
  122.      int signal;
  123. {
  124.   errno = 0;
  125.   if (remote_debugging)
  126.     remote_resume (step, signal);
  127.   else
  128.     {
  129.       ptrace (step ? 9 : 7, inferior_pid, 1, signal);
  130.       if (errno)
  131.     perror_with_name ("ptrace");
  132.     }
  133. }
  134.  
  135. void
  136. fetch_inferior_registers ()
  137. {
  138.   register int regno;
  139.   register unsigned int regaddr;
  140.   char buf[MAX_REGISTER_RAW_SIZE];
  141.   register int i;
  142.  
  143.   struct user u;
  144.   unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
  145.   offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  146.  
  147.   for (regno = 0; regno < NUM_REGS; regno++)
  148.     {
  149.       regaddr = register_addr (regno, offset);
  150.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  151.      {
  152.        *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
  153.        regaddr += sizeof (int);
  154.      }
  155.       supply_register (regno, buf);
  156.     }
  157. }
  158.  
  159. /* Store our register values back into the inferior.
  160.    If REGNO is -1, do this for all registers.
  161.    Otherwise, REGNO specifies which register (so we can save time).  */
  162.  
  163. store_inferior_registers (regno)
  164.      int regno;
  165. {
  166.   register unsigned int regaddr;
  167.   char buf[80];
  168.  
  169.   struct user u;
  170.   unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
  171.   offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  172.  
  173.   if (regno >= 0)
  174.     {
  175.       regaddr = register_addr (regno, offset);
  176.       errno = 0;
  177.       ptrace (6, inferior_pid, regaddr, read_register (regno));
  178.       if (errno != 0)
  179.     {
  180.       sprintf (buf, "writing register number %d", regno);
  181.       perror_with_name (buf);
  182.     }
  183.     }
  184.   else for (regno = 0; regno < NUM_REGS; regno++)
  185.     {
  186.       regaddr = register_addr (regno, offset);
  187.       errno = 0;
  188.       ptrace (6, inferior_pid, regaddr, read_register (regno));
  189.       if (errno != 0)
  190.     {
  191.       sprintf (buf, "writing register number %d", regno);
  192.       perror_with_name (buf);
  193.     }
  194.     }
  195. }
  196.  
  197. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  198.    to debugger memory starting at MYADDR. 
  199.    On failure (cannot read from inferior, usually because address is out
  200.    of bounds) returns the value of errno. */
  201.  
  202. int
  203. read_inferior_memory (memaddr, myaddr, len)
  204.      CORE_ADDR memaddr;
  205.      char *myaddr;
  206.      int len;
  207. {
  208.   register int i;
  209.   /* Round starting address down to longword boundary.  */
  210.   register CORE_ADDR addr = memaddr & - sizeof (int);
  211.   /* Round ending address up; get number of longwords that makes.  */
  212.   register int count
  213.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  214.   /* Allocate buffer of that many longwords.  */
  215.   register int *buffer = (int *) alloca (count * sizeof (int));
  216.   extern int errno;
  217.  
  218.   /* Read all the longwords */
  219.   for (i = 0; i < count; i++, addr += sizeof (int))
  220.     {
  221.       errno = 0;
  222.       if (remote_debugging)
  223.     buffer[i] = remote_fetch_word (addr);
  224.       else
  225.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  226.       if (errno)
  227.     return errno;
  228.     }
  229.  
  230.   /* Copy appropriate bytes out of the buffer.  */
  231.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  232.   return 0;
  233. }
  234.  
  235. /* Copy LEN bytes of data from debugger memory at MYADDR
  236.    to inferior's memory at MEMADDR.
  237.    On failure (cannot write the inferior)
  238.    returns the value of errno.  */
  239.  
  240. int
  241. write_inferior_memory (memaddr, myaddr, len)
  242.      CORE_ADDR memaddr;
  243.      char *myaddr;
  244.      int len;
  245. {
  246.   register int i;
  247.   /* Round starting address down to longword boundary.  */
  248.   register CORE_ADDR addr = memaddr & - sizeof (int);
  249.   /* Round ending address up; get number of longwords that makes.  */
  250.   register int count
  251.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  252.   /* Allocate buffer of that many longwords.  */
  253.   register int *buffer = (int *) alloca (count * sizeof (int));
  254.   extern int errno;
  255.  
  256.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  257.  
  258.   if (remote_debugging)
  259.     buffer[0] = remote_fetch_word (addr);
  260.   else
  261.     buffer[0] = ptrace (1, inferior_pid, addr, 0);
  262.  
  263.   if (count > 1)
  264.     {
  265.       if (remote_debugging)
  266.     buffer[count - 1]
  267.       = remote_fetch_word (addr + (count - 1) * sizeof (int));
  268.       else
  269.     buffer[count - 1]
  270.       = ptrace (1, inferior_pid,
  271.             addr + (count - 1) * sizeof (int), 0);
  272.     }
  273.  
  274.   /* Copy data to be written over corresponding part of buffer */
  275.  
  276.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  277.  
  278.   /* Write the entire buffer.  */
  279.  
  280.   for (i = 0; i < count; i++, addr += sizeof (int))
  281.     {
  282.       errno = 0;
  283.       if (remote_debugging)
  284.     remote_store_word (addr, buffer[i]);
  285.       else
  286.     ptrace (4, inferior_pid, addr, buffer[i]);
  287.       if (errno)
  288.     return errno;
  289.     }
  290.  
  291.   return 0;
  292. }
  293.  
  294. /* Work with core dump and executable files, for GDB. 
  295.    This code would be in core.c if it weren't machine-dependent. */
  296.  
  297. #ifndef N_TXTADDR
  298. #define N_TXTADDR(hdr) 0
  299. #endif /* no N_TXTADDR */
  300.  
  301. #ifndef N_DATADDR
  302. #define N_DATADDR(hdr) hdr.a_text
  303. #endif /* no N_DATADDR */
  304.  
  305. /* Make COFF and non-COFF names for things a little more compatible
  306.    to reduce conditionals later.  */
  307.  
  308. #ifndef COFF_FORMAT
  309. #ifndef AOUTHDR
  310. #define AOUTHDR struct exec
  311. #endif
  312. #endif
  313.  
  314. extern char *sys_siglist[];
  315.  
  316.  
  317. /* Hook for `exec_file_command' command to call.  */
  318.  
  319. extern void (*exec_file_display_hook) ();
  320.    
  321. /* File names of core file and executable file.  */
  322.  
  323. extern char *corefile;
  324. extern char *execfile;
  325.  
  326. /* Descriptors on which core file and executable file are open.
  327.    Note that the execchan is closed when an inferior is created
  328.    and reopened if the inferior dies or is killed.  */
  329.  
  330. extern int corechan;
  331. extern int execchan;
  332.  
  333. /* Last modification time of executable file.
  334.    Also used in source.c to compare against mtime of a source file.  */
  335.  
  336. extern int exec_mtime;
  337.  
  338. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  339.  
  340. extern CORE_ADDR data_start;
  341. extern CORE_ADDR data_end;
  342. extern CORE_ADDR stack_start;
  343. extern CORE_ADDR stack_end;
  344.  
  345. /* Virtual addresses of bounds of two areas of memory in the exec file.
  346.    Note that the data area in the exec file is used only when there is no core file.  */
  347.  
  348. extern CORE_ADDR text_start;
  349. extern CORE_ADDR text_end;
  350.  
  351. extern CORE_ADDR exec_data_start;
  352. extern CORE_ADDR exec_data_end;
  353.  
  354. /* Address in executable file of start of text area data.  */
  355.  
  356. extern int text_offset;
  357.  
  358. /* Address in executable file of start of data area data.  */
  359.  
  360. extern int exec_data_offset;
  361.  
  362. /* Address in core file of start of data area data.  */
  363.  
  364. extern int data_offset;
  365.  
  366. /* Address in core file of start of stack area data.  */
  367.  
  368. extern int stack_offset;
  369.  
  370. #ifdef COFF_FORMAT
  371. /* various coff data structures */
  372.  
  373. extern FILHDR file_hdr;
  374. extern SCNHDR text_hdr;
  375. extern SCNHDR data_hdr;
  376.  
  377. #endif /* not COFF_FORMAT */
  378.  
  379. /* a.out header saved in core file.  */
  380.   
  381. extern AOUTHDR core_aouthdr;
  382.  
  383. /* a.out header of exec file.  */
  384.  
  385. extern AOUTHDR exec_aouthdr;
  386.  
  387. extern void validate_files ();
  388.  
  389. core_file_command (filename, from_tty)
  390.      char *filename;
  391.      int from_tty;
  392. {
  393.   int val;
  394.   extern char registers[];
  395.  
  396.   /* Discard all vestiges of any previous core file
  397.      and mark data and stack spaces as empty.  */
  398.  
  399.   if (corefile)
  400.     free (corefile);
  401.   corefile = 0;
  402.  
  403.   if (corechan >= 0)
  404.     close (corechan);
  405.   corechan = -1;
  406.  
  407.   data_start = 0;
  408.   data_end = 0;
  409.   stack_start = STACK_END_ADDR;
  410.   stack_end = STACK_END_ADDR;
  411.  
  412.   /* Now, if a new core file was specified, open it and digest it.  */
  413.  
  414.   if (filename)
  415.     {
  416.       filename = tilde_expand (filename);
  417.       make_cleanup (free, filename);
  418.       
  419.       if (have_inferior_p ())
  420.     error ("To look at a core file, you must kill the inferior with \"kill\".");
  421.       corechan = open (filename, O_RDONLY, 0);
  422.       if (corechan < 0)
  423.     perror_with_name (filename);
  424.       /* 4.2-style (and perhaps also sysV-style) core dump file.  */
  425.       {
  426.     struct user u;
  427.  
  428.     int reg_offset;
  429.  
  430.     val = myread (corechan, &u, sizeof u);
  431.     if (val < 0)
  432.       perror_with_name (filename);
  433.     data_start = exec_data_start;
  434.  
  435.     data_end = data_start + NBPG * u.u_dsize;
  436.     stack_start = stack_end - NBPG * u.u_ssize;
  437.     data_offset = NBPG * UPAGES;
  438.     stack_offset = NBPG * (UPAGES + u.u_dsize);
  439.     reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
  440.  
  441.     /* I don't know where to find this info.
  442.        So, for now, mark it as not available.  */
  443. /*    N_SET_MAGIC (core_aouthdr, 0);  */
  444.     bzero ((char *) &core_aouthdr, sizeof core_aouthdr);
  445.  
  446.     /* Read the register values out of the core file and store
  447.        them where `read_register' will find them.  */
  448.  
  449.     {
  450.       register int regno;
  451.  
  452.       for (regno = 0; regno < NUM_REGS; regno++)
  453.         {
  454.           char buf[MAX_REGISTER_RAW_SIZE];
  455.  
  456.           val = lseek (corechan, register_addr (regno, reg_offset), 0);
  457.           if (val < 0)
  458.         perror_with_name (filename);
  459.  
  460.            val = myread (corechan, buf, sizeof buf);
  461.           if (val < 0)
  462.         perror_with_name (filename);
  463.           supply_register (regno, buf);
  464.         }
  465.     }
  466.       }
  467.       if (filename[0] == '/')
  468.     corefile = savestring (filename, strlen (filename));
  469.       else
  470.     {
  471.       corefile = concat (current_directory, "/", filename);
  472.     }
  473.  
  474.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  475.                         read_pc ()));
  476.       select_frame (get_current_frame (), 0);
  477.       validate_files ();
  478.     }
  479.   else if (from_tty)
  480.     printf ("No core file now.\n");
  481. }
  482.  
  483. exec_file_command (filename, from_tty)
  484.      char *filename;
  485.      int from_tty;
  486. {
  487.   int val;
  488.  
  489.   /* Eliminate all traces of old exec file.
  490.      Mark text segment as empty.  */
  491.  
  492.   if (execfile)
  493.     free (execfile);
  494.   execfile = 0;
  495.   data_start = 0;
  496.   data_end -= exec_data_start;
  497.   text_start = 0;
  498.   text_end = 0;
  499.   exec_data_start = 0;
  500.   exec_data_end = 0;
  501.   if (execchan >= 0)
  502.     close (execchan);
  503.   execchan = -1;
  504.  
  505.   /* Now open and digest the file the user requested, if any.  */
  506.  
  507.   if (filename)
  508.     {
  509.       filename = tilde_expand (filename);
  510.       make_cleanup (free, filename);
  511.       
  512.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  513.             &execfile);
  514.       if (execchan < 0)
  515.     perror_with_name (filename);
  516.  
  517. #ifdef COFF_FORMAT
  518.       {
  519.     int aout_hdrsize;
  520.     int num_sections;
  521.  
  522.     if (read_file_hdr (execchan, &file_hdr) < 0)
  523.       error ("\"%s\": not in executable format.", execfile);
  524.  
  525.     aout_hdrsize = file_hdr.f_opthdr;
  526.     num_sections = file_hdr.f_nscns;
  527.  
  528.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  529.       error ("\"%s\": can't read optional aouthdr", execfile);
  530.  
  531.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  532.                   aout_hdrsize) < 0)
  533.       error ("\"%s\": can't read text section header", execfile);
  534.  
  535.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  536.                   aout_hdrsize) < 0)
  537.       error ("\"%s\": can't read data section header", execfile);
  538.  
  539.     text_start = exec_aouthdr.text_start;
  540.     text_end = text_start + exec_aouthdr.tsize;
  541.     text_offset = text_hdr.s_scnptr;
  542.     exec_data_start = exec_aouthdr.data_start;
  543.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  544.     exec_data_offset = data_hdr.s_scnptr;
  545.     data_start = exec_data_start;
  546.     data_end += exec_data_start;
  547.     exec_mtime = file_hdr.f_timdat;
  548.       }
  549. #else /* not COFF_FORMAT */
  550.       {
  551.     struct stat st_exec;
  552.  
  553. #ifdef HEADER_SEEK_FD
  554.     HEADER_SEEK_FD (execchan);
  555. #endif
  556.  
  557.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  558.  
  559.     if (val < 0)
  560.       perror_with_name (filename);
  561.  
  562.         text_start = N_TXTADDR (exec_aouthdr);
  563.         exec_data_start = N_DATADDR (exec_aouthdr);
  564.  
  565.     text_offset = N_TXTOFF (exec_aouthdr);
  566.     exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  567.  
  568.     text_end = text_start + exec_aouthdr.a_text;
  569.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  570.     data_start = exec_data_start;
  571.     data_end += exec_data_start;
  572.  
  573.     fstat (execchan, &st_exec);
  574.     exec_mtime = st_exec.st_mtime;
  575.       }
  576. #endif /* not COFF_FORMAT */
  577.  
  578.       validate_files ();
  579.     }
  580.   else if (from_tty)
  581.     printf ("No exec file now.\n");
  582.  
  583.   /* Tell display code (if any) about the changed file name.  */
  584.   if (exec_file_display_hook)
  585.     (*exec_file_display_hook) (filename);
  586. }
  587.  
  588. /* helper functions for m-i386.h */
  589.  
  590. /* stdio style buffering to minimize calls to ptrace */
  591. static CORE_ADDR codestream_next_addr;
  592. static CORE_ADDR codestream_addr;
  593. static unsigned char codestream_buf[sizeof (int)];
  594. static int codestream_off;
  595. static int codestream_cnt;
  596.  
  597. #define codestream_tell() (codestream_addr + codestream_off)
  598. #define codestream_peek() (codestream_cnt == 0 ? \
  599.                codestream_fill(1): codestream_buf[codestream_off])
  600. #define codestream_get() (codestream_cnt-- == 0 ? \
  601.              codestream_fill(0) : codestream_buf[codestream_off++])
  602.  
  603. static unsigned char 
  604. codestream_fill (peek_flag)
  605. {
  606.   codestream_addr = codestream_next_addr;
  607.   codestream_next_addr += sizeof (int);
  608.   codestream_off = 0;
  609.   codestream_cnt = sizeof (int);
  610.   read_memory (codestream_addr,
  611.            (unsigned char *)codestream_buf,
  612.            sizeof (int));
  613.   
  614.   if (peek_flag)
  615.     return (codestream_peek());
  616.   else
  617.     return (codestream_get());
  618. }
  619.  
  620. static void
  621. codestream_seek (place)
  622. {
  623.   codestream_next_addr = place & -sizeof (int);
  624.   codestream_cnt = 0;
  625.   codestream_fill (1);
  626.   while (codestream_tell() != place)
  627.     codestream_get ();
  628. }
  629.  
  630. static void
  631. codestream_read (buf, count)
  632.      unsigned char *buf;
  633. {
  634.   unsigned char *p;
  635.   int i;
  636.   p = buf;
  637.   for (i = 0; i < count; i++)
  638.     *p++ = codestream_get ();
  639. }
  640.  
  641. /* next instruction is a jump, move to target */
  642. static
  643. i386_follow_jump ()
  644. {
  645.   int long_delta;
  646.   short short_delta;
  647.   char byte_delta;
  648.   int data16;
  649.   int pos;
  650.   
  651.   pos = codestream_tell ();
  652.   
  653.   data16 = 0;
  654.   if (codestream_peek () == 0x66)
  655.     {
  656.       codestream_get ();
  657.       data16 = 1;
  658.     }
  659.   
  660.   switch (codestream_get ())
  661.     {
  662.     case 0xe9:
  663.       /* relative jump: if data16 == 0, disp32, else disp16 */
  664.       if (data16)
  665.     {
  666.       codestream_read ((unsigned char *)&short_delta, 2);
  667.       pos += short_delta + 3; /* include size of jmp inst */
  668.     }
  669.       else
  670.     {
  671.       codestream_read ((unsigned char *)&long_delta, 4);
  672.       pos += long_delta + 5;
  673.     }
  674.       break;
  675.     case 0xeb:
  676.       /* relative jump, disp8 (ignore data16) */
  677.       codestream_read ((unsigned char *)&byte_delta, 1);
  678.       pos += byte_delta + 2;
  679.       break;
  680.     }
  681.   codestream_seek (pos + data16);
  682. }
  683.  
  684. /*
  685.  * find & return amound a local space allocated, and advance codestream to
  686.  * first register push (if any)
  687.  *
  688.  * if entry sequence doesn't make sense, return -1, and leave 
  689.  * codestream pointer random
  690.  */
  691. static long
  692. i386_get_frame_setup (pc)
  693. {
  694.   unsigned char op;
  695.   
  696.   codestream_seek (pc);
  697.   
  698.   i386_follow_jump ();
  699.   
  700.   op = codestream_get ();
  701.   
  702.   if (op == 0x58)        /* popl %eax */
  703.     {
  704.       /*
  705.        * this function must start with
  706.        * 
  707.        *    popl %eax          0x58
  708.        *    xchgl %eax, (%esp)  0x87 0x04 0x24
  709.        * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
  710.        *
  711.        * (the system 5 compiler puts out the second xchg
  712.        * inst, and the assembler doesn't try to optimize it,
  713.        * so the 'sib' form gets generated)
  714.        * 
  715.        * this sequence is used to get the address of the return
  716.        * buffer for a function that returns a structure
  717.        */
  718.       int pos;
  719.       unsigned char buf[4];
  720.       static unsigned char proto1[3] = { 0x87,0x04,0x24 };
  721.       static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
  722.       pos = codestream_tell ();
  723.       codestream_read (buf, 4);
  724.       if (bcmp (buf, proto1, 3) == 0)
  725.     pos += 3;
  726.       else if (bcmp (buf, proto2, 4) == 0)
  727.     pos += 4;
  728.       
  729.       codestream_seek (pos);
  730.       op = codestream_get (); /* update next opcode */
  731.     }
  732.   
  733.   if (op == 0x55)        /* pushl %esp */
  734.     {            
  735.       /* check for movl %esp, %ebp - can be written two ways */
  736.       switch (codestream_get ())
  737.     {
  738.     case 0x8b:
  739.       if (codestream_get () != 0xec)
  740.         return (-1);
  741.       break;
  742.     case 0x89:
  743.       if (codestream_get () != 0xe5)
  744.         return (-1);
  745.       break;
  746.     default:
  747.       return (-1);
  748.     }
  749.       /* check for stack adjustment 
  750.        *
  751.        *  subl $XXX, %esp
  752.        *
  753.        * note: you can't subtract a 16 bit immediate
  754.        * from a 32 bit reg, so we don't have to worry
  755.        * about a data16 prefix 
  756.        */
  757.       op = codestream_peek ();
  758.       if (op == 0x83)
  759.     {
  760.       /* subl with 8 bit immed */
  761.       codestream_get ();
  762.       if (codestream_get () != 0xec)
  763.         return (-1);
  764.       /* subl with signed byte immediate 
  765.        * (though it wouldn't make sense to be negative)
  766.        */
  767.       return (codestream_get());
  768.     }
  769.       else if (op == 0x81)
  770.     {
  771.       /* subl with 32 bit immed */
  772.       int locals;
  773.       codestream_get();
  774.       if (codestream_get () != 0xec)
  775.         return (-1);
  776.       /* subl with 32 bit immediate */
  777.       codestream_read ((unsigned char *)&locals, 4);
  778.       return (locals);
  779.     }
  780.       else
  781.     {
  782.       return (0);
  783.     }
  784.     }
  785.   else if (op == 0xc8)
  786.     {
  787.       /* enter instruction: arg is 16 bit unsigned immed */
  788.       unsigned short slocals;
  789.       codestream_read ((unsigned char *)&slocals, 2);
  790.       codestream_get (); /* flush final byte of enter instruction */
  791.       return (slocals);
  792.     }
  793.   return (-1);
  794. }
  795.  
  796. /* Return number of args passed to a frame.
  797.    Can return -1, meaning no way to tell.  */
  798.  
  799. /* on the 386, the instruction following the call could be:
  800.  *  popl %ecx        -  one arg
  801.  *  addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
  802.  *  anything else    -  zero args
  803.  */
  804.  
  805. int
  806. i386_frame_num_args (fi)
  807.      struct frame_info fi;
  808. {
  809.   int retpc;                        
  810.   unsigned char op;                    
  811.   struct frame_info *pfi;
  812.  
  813.   pfi = get_prev_frame_info ((fi));            
  814.   if (pfi == 0)
  815.     {
  816.       /* Note:  this can happen if we are looking at the frame for
  817.      main, because FRAME_CHAIN_VALID won't let us go into
  818.      start.  If we have debugging symbols, that's not really
  819.      a big deal; it just means it will only show as many arguments
  820.      to main as are declared.  */
  821.       return -1;
  822.     }
  823.   else
  824.     {
  825.       retpc = pfi->pc;                    
  826.       op = read_memory_integer (retpc, 1);            
  827.       if (op == 0x59)                    
  828.     /* pop %ecx */                   
  829.     return 1;                
  830.       else if (op == 0x83)
  831.     {
  832.       op = read_memory_integer (retpc+1, 1);    
  833.       if (op == 0xc4)                
  834.         /* addl $<signed imm 8 bits>, %esp */    
  835.         return (read_memory_integer (retpc+2,1)&0xff)/4;
  836.       else
  837.         return 0;
  838.     }
  839.       else if (op == 0x81)
  840.     { /* add with 32 bit immediate */
  841.       op = read_memory_integer (retpc+1, 1);    
  842.       if (op == 0xc4)                
  843.         /* addl $<imm 32>, %esp */        
  844.         return read_memory_integer (retpc+2, 4) / 4;
  845.       else
  846.         return 0;
  847.     }
  848.       else
  849.     {
  850.       return 0;
  851.     }
  852.     }
  853. }
  854.  
  855. /*
  856.  * parse the first few instructions of the function to see
  857.  * what registers were stored.
  858.  *
  859.  * We handle these cases:
  860.  *
  861.  * The startup sequence can be at the start of the function,
  862.  * or the function can start with a branch to startup code at the end.
  863.  *
  864.  * %ebp can be set up with either the 'enter' instruction, or 
  865.  * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
  866.  * but was once used in the sys5 compiler)
  867.  *
  868.  * Local space is allocated just below the saved %ebp by either the
  869.  * 'enter' instruction, or by 'subl $<size>, %esp'.  'enter' has
  870.  * a 16 bit unsigned argument for space to allocate, and the
  871.  * 'addl' instruction could have either a signed byte, or
  872.  * 32 bit immediate.
  873.  *
  874.  * Next, the registers used by this function are pushed.  In
  875.  * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
  876.  * (and sometimes a harmless bug causes it to also save but not restore %eax);
  877.  * however, the code below is willing to see the pushes in any order,
  878.  * and will handle up to 8 of them.
  879.  *
  880.  * If the setup sequence is at the end of the function, then the
  881.  * next instruction will be a branch back to the start.
  882.  */
  883.  
  884. i386_frame_find_saved_regs (fip, fsrp)
  885.      struct frame_info *fip;
  886.      struct frame_saved_regs *fsrp;
  887. {
  888.   unsigned long locals;
  889.   unsigned char *p;
  890.   unsigned char op;
  891.   CORE_ADDR dummy_bottom;
  892.   CORE_ADDR adr;
  893.   int i;
  894.   
  895.   bzero (fsrp, sizeof *fsrp);
  896.   
  897.   /* if frame is the end of a dummy, compute where the
  898.    * beginning would be
  899.    */
  900.   dummy_bottom = fip->frame - 4 - NUM_REGS*4 - CALL_DUMMY_LENGTH;
  901.   
  902.   /* check if the PC is in the stack, in a dummy frame */
  903.   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
  904.     {
  905.       /* all regs were saved by push_call_dummy () */
  906.       adr = fip->frame - 4;
  907.       for (i = 0; i < NUM_REGS; i++) 
  908.     {
  909.       fsrp->regs[i] = adr;
  910.       adr -= 4;
  911.     }
  912.       return;
  913.     }
  914.   
  915.   locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
  916.   
  917.   if (locals >= 0) 
  918.     {
  919.       adr = fip->frame - 4 - locals;
  920.       for (i = 0; i < 8; i++) 
  921.     {
  922.       op = codestream_get ();
  923.       if (op < 0x50 || op > 0x57)
  924.         break;
  925.       fsrp->regs[op - 0x50] = adr;
  926.       adr -= 4;
  927.     }
  928.     }
  929.   
  930.   fsrp->regs[PC_REGNUM] = fip->frame + 4;
  931.   fsrp->regs[FP_REGNUM] = fip->frame;
  932. }
  933.  
  934. /* return pc of first real instruction */
  935. i386_skip_prologue (pc)
  936. {
  937.   unsigned char op;
  938.   int i;
  939.   
  940.   if (i386_get_frame_setup (pc) < 0)
  941.     return (pc);
  942.   
  943.   /* found valid frame setup - codestream now points to 
  944.    * start of push instructions for saving registers
  945.    */
  946.   
  947.   /* skip over register saves */
  948.   for (i = 0; i < 8; i++)
  949.     {
  950.       op = codestream_peek ();
  951.       /* break if not pushl inst */
  952.       if (op < 0x50 || op > 0x57) 
  953.     break;
  954.       codestream_get ();
  955.     }
  956.   
  957.   i386_follow_jump ();
  958.   
  959.   return (codestream_tell ());
  960. }
  961.  
  962. i386_push_dummy_frame ()
  963. {
  964.   CORE_ADDR sp = read_register (SP_REGNUM);
  965.   int regnum;
  966.   
  967.   sp = push_word (sp, read_register (PC_REGNUM));
  968.   sp = push_word (sp, read_register (FP_REGNUM));
  969.   write_register (FP_REGNUM, sp);
  970.   for (regnum = 0; regnum < NUM_REGS; regnum++)
  971.     sp = push_word (sp, read_register (regnum));
  972.   write_register (SP_REGNUM, sp);
  973. }
  974.  
  975. i386_pop_frame ()
  976. {
  977.   FRAME frame = get_current_frame ();
  978.   CORE_ADDR fp;
  979.   int regnum;
  980.   struct frame_saved_regs fsr;
  981.   struct frame_info *fi;
  982.   
  983.   fi = get_frame_info (frame);
  984.   fp = fi->frame;
  985.   get_frame_saved_regs (fi, &fsr);
  986.   for (regnum = 0; regnum < NUM_REGS; regnum++) 
  987.     {
  988.       CORE_ADDR adr;
  989.       adr = fsr.regs[regnum];
  990.       if (adr)
  991.     write_register (regnum, read_memory_integer (adr, 4));
  992.     }
  993.   write_register (FP_REGNUM, read_memory_integer (fp, 4));
  994.   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
  995.   write_register (SP_REGNUM, fp + 8);
  996.   flush_cached_frames ();
  997.   set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  998.                     read_pc ()));
  999. }
  1000.  
  1001. /* this table must line up with REGISTER_NAMES in m-i386.h */
  1002. /* symbols like 'EAX' come from <sys/reg.h> */
  1003. static int regmap[] = 
  1004. {
  1005.   EAX, ECX, EDX, EBX,
  1006.   UESP, EBP, ESI, EDI,
  1007.   EIP, EFL, CS, SS,
  1008.   DS, ES, FS, GS,
  1009. };
  1010.  
  1011. /* blockend is the value of u.u_ar0, and points to the
  1012.  * place where GS is stored
  1013.  */
  1014. i386_register_u_addr (blockend, regnum)
  1015. {
  1016. #if 0
  1017.   /* this will be needed if fp registers are reinstated */
  1018.   /* for now, you can look at them with 'info float'
  1019.    * sys5 wont let you change them with ptrace anyway
  1020.    */
  1021.   if (regnum >= FP0_REGNUM && regnum <= FP7_REGNUM) 
  1022.     {
  1023.       int ubase, fpstate;
  1024.       struct user u;
  1025.       ubase = blockend + 4 * (SS + 1) - KSTKSZ;
  1026.       fpstate = ubase + ((char *)&u.u_fpstate - (char *)&u);
  1027.       return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
  1028.     } 
  1029.   else
  1030. #endif
  1031.     return (blockend + 4 * regmap[regnum]);
  1032.   
  1033. }
  1034.  
  1035. i387_to_double (from, to)
  1036.      char *from;
  1037.      char *to;
  1038. {
  1039.   long *lp;
  1040.   /* push extended mode on 387 stack, then pop in double mode
  1041.    *
  1042.    * first, set exception masks so no error is generated -
  1043.    * number will be rounded to inf or 0, if necessary 
  1044.    */
  1045.   asm ("pushl %eax");         /* grab a stack slot */
  1046.   asm ("fstcw (%esp)");        /* get 387 control word */
  1047.   asm ("movl (%esp),%eax");    /* save old value */
  1048.   asm ("orl $0x3f,%eax");        /* mask all exceptions */
  1049.   asm ("pushl %eax");
  1050.   asm ("fldcw (%esp)");        /* load new value into 387 */
  1051.   
  1052.   asm ("movl 8(%ebp),%eax");
  1053.   asm ("fldt (%eax)");        /* push extended number on 387 stack */
  1054.   asm ("fwait");
  1055.   asm ("movl 12(%ebp),%eax");
  1056.   asm ("fstpl (%eax)");        /* pop double */
  1057.   asm ("fwait");
  1058.   
  1059.   asm ("popl %eax");        /* flush modified control word */
  1060.   asm ("fnclex");            /* clear exceptions */
  1061.   asm ("fldcw (%esp)");        /* restore original control word */
  1062.   asm ("popl %eax");        /* flush saved copy */
  1063. }
  1064.  
  1065. double_to_i387 (from, to)
  1066.      char *from;
  1067.      char *to;
  1068. {
  1069.   /* push double mode on 387 stack, then pop in extended mode
  1070.    * no errors are possible because every 64-bit pattern
  1071.    * can be converted to an extended
  1072.    */
  1073.   asm ("movl 8(%ebp),%eax");
  1074.   asm ("fldl (%eax)");
  1075.   asm ("fwait");
  1076.   asm ("movl 12(%ebp),%eax");
  1077.   asm ("fstpt (%eax)");
  1078.   asm ("fwait");
  1079. }
  1080.  
  1081. struct env387 
  1082. {
  1083.   unsigned short control;
  1084.   unsigned short r0;
  1085.   unsigned short status;
  1086.   unsigned short r1;
  1087.   unsigned short tag;
  1088.   unsigned short r2;
  1089.   unsigned long eip;
  1090.   unsigned short code_seg;
  1091.   unsigned short opcode;
  1092.   unsigned long operand;
  1093.   unsigned short operand_seg;
  1094.   unsigned short r3;
  1095.   unsigned char regs[8][10];
  1096. };
  1097.  
  1098. static
  1099. print_387_control_word (control)
  1100. unsigned short control;
  1101. {
  1102.   printf ("control 0x%04x: ", control);
  1103.   printf ("compute to ");
  1104.   switch ((control >> 8) & 3) 
  1105.     {
  1106.     case 0: printf ("24 bits; "); break;
  1107.     case 1: printf ("(bad); "); break;
  1108.     case 2: printf ("53 bits; "); break;
  1109.     case 3: printf ("64 bits; "); break;
  1110.     }
  1111.   printf ("round ");
  1112.   switch ((control >> 10) & 3) 
  1113.     {
  1114.     case 0: printf ("NEAREST; "); break;
  1115.     case 1: printf ("DOWN; "); break;
  1116.     case 2: printf ("UP; "); break;
  1117.     case 3: printf ("CHOP; "); break;
  1118.     }
  1119.   if (control & 0x3f) 
  1120.     {
  1121.       printf ("mask:");
  1122.       if (control & 0x0001) printf (" INVALID");
  1123.       if (control & 0x0002) printf (" DENORM");
  1124.       if (control & 0x0004) printf (" DIVZ");
  1125.       if (control & 0x0008) printf (" OVERF");
  1126.       if (control & 0x0010) printf (" UNDERF");
  1127.       if (control & 0x0020) printf (" LOS");
  1128.       printf (";");
  1129.     }
  1130.   printf ("\n");
  1131.   if (control & 0xe080) printf ("warning: reserved bits on 0x%x\n",
  1132.                 control & 0xe080);
  1133. }
  1134.  
  1135. static
  1136. print_387_status_word (status)
  1137.      unsigned short status;
  1138. {
  1139.   printf ("status 0x%04x: ", status);
  1140.   if (status & 0xff) 
  1141.     {
  1142.       printf ("exceptions:");
  1143.       if (status & 0x0001) printf (" INVALID");
  1144.       if (status & 0x0002) printf (" DENORM");
  1145.       if (status & 0x0004) printf (" DIVZ");
  1146.       if (status & 0x0008) printf (" OVERF");
  1147.       if (status & 0x0010) printf (" UNDERF");
  1148.       if (status & 0x0020) printf (" LOS");
  1149.       if (status & 0x0040) printf (" FPSTACK");
  1150.       printf ("; ");
  1151.     }
  1152.   printf ("flags: %d%d%d%d; ",
  1153.       (status & 0x4000) != 0,
  1154.       (status & 0x0400) != 0,
  1155.       (status & 0x0200) != 0,
  1156.       (status & 0x0100) != 0);
  1157.   
  1158.   printf ("top %d\n", (status >> 11) & 7);
  1159. }
  1160.  
  1161. static
  1162. print_387_status (status, ep)
  1163.      unsigned short status;
  1164.      struct env387 *ep;
  1165. {
  1166.   int i;
  1167.   int bothstatus;
  1168.   int top;
  1169.   int fpreg;
  1170.   unsigned char *p;
  1171.   
  1172.   bothstatus = ((status != 0) && (ep->status != 0));
  1173.   if (status != 0) 
  1174.     {
  1175.       if (bothstatus)
  1176.     printf ("u: ");
  1177.       print_387_status_word (status);
  1178.     }
  1179.   
  1180.   if (ep->status != 0) 
  1181.     {
  1182.       if (bothstatus)
  1183.     printf ("e: ");
  1184.       print_387_status_word (ep->status);
  1185.     }
  1186.   
  1187.   print_387_control_word (ep->control);
  1188.   printf ("last exception: ");
  1189.   printf ("opcode 0x%x; ", ep->opcode);
  1190.   printf ("pc 0x%x:0x%x; ", ep->code_seg, ep->eip);
  1191.   printf ("operand 0x%x:0x%x\n", ep->operand_seg, ep->operand);
  1192.   
  1193.   top = (ep->status >> 11) & 7;
  1194.   
  1195.   printf ("regno  tag  msb              lsb  value\n");
  1196.   for (fpreg = 7; fpreg >= 0; fpreg--) 
  1197.     {
  1198.       double val;
  1199.       
  1200.       printf ("%s %d: ", fpreg == top ? "=>" : "  ", fpreg);
  1201.       
  1202.       switch ((ep->tag >> (fpreg * 2)) & 3) 
  1203.     {
  1204.     case 0: printf ("valid "); break;
  1205.     case 1: printf ("zero  "); break;
  1206.     case 2: printf ("trap  "); break;
  1207.     case 3: printf ("empty "); break;
  1208.     }
  1209.       for (i = 9; i >= 0; i--)
  1210.     printf ("%02x", ep->regs[fpreg][i]);
  1211.       
  1212.       i387_to_double (ep->regs[fpreg], (char *)&val);
  1213.       printf ("  %g\n", val);
  1214.     }
  1215.   if (ep->r0)
  1216.     printf ("warning: reserved0 is 0x%x\n", ep->r0);
  1217.   if (ep->r1)
  1218.     printf ("warning: reserved1 is 0x%x\n", ep->r1);
  1219.   if (ep->r2)
  1220.     printf ("warning: reserved2 is 0x%x\n", ep->r2);
  1221.   if (ep->r3)
  1222.     printf ("warning: reserved3 is 0x%x\n", ep->r3);
  1223. }
  1224.  
  1225. #ifndef U_FPSTATE
  1226. #define U_FPSTATE(u) u.u_fpstate
  1227. #endif
  1228.  
  1229. i386_float_info ()
  1230. {
  1231.   struct user u; /* just for address computations */
  1232.   int i;
  1233.   /* fpstate defined in <sys/user.h> */
  1234.   struct fpstate *fpstatep;
  1235.   char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
  1236.   unsigned int uaddr;
  1237.   char fpvalid;
  1238.   unsigned int rounded_addr;
  1239.   unsigned int rounded_size;
  1240.   extern int corechan;
  1241.   int skip;
  1242.   
  1243.   uaddr = (char *)&u.u_fpvalid - (char *)&u;
  1244.   if (have_inferior_p()) 
  1245.     {
  1246.       unsigned int data;
  1247.       unsigned int mask;
  1248.       
  1249.       rounded_addr = uaddr & -sizeof (int);
  1250.       data = ptrace (3, inferior_pid, rounded_addr, 0);
  1251.       mask = 0xff << ((uaddr - rounded_addr) * 8);
  1252.       
  1253.       fpvalid = ((data & mask) != 0);
  1254.     } 
  1255.   else 
  1256.     {
  1257.       if (lseek (corechan, uaddr, 0) < 0)
  1258.     perror ("seek on core file");
  1259.       if (myread (corechan, &fpvalid, 1) < 0) 
  1260.     perror ("read on core file");
  1261.       
  1262.     }
  1263.   
  1264.   if (fpvalid == 0) 
  1265.     {
  1266.       printf ("no floating point status saved\n");
  1267.       return;
  1268.     }
  1269.   
  1270.   uaddr = (char *)&U_FPSTATE(u) - (char *)&u;
  1271.   if (have_inferior_p ()) 
  1272.     {
  1273.       int *ip;
  1274.       
  1275.       rounded_addr = uaddr & -sizeof (int);
  1276.       rounded_size = (((uaddr + sizeof (struct fpstate)) - uaddr) +
  1277.               sizeof (int) - 1) / sizeof (int);
  1278.       skip = uaddr - rounded_addr;
  1279.       
  1280.       ip = (int *)buf;
  1281.       for (i = 0; i < rounded_size; i++) 
  1282.     {
  1283.       *ip++ = ptrace (3, inferior_pid, rounded_addr, 0);
  1284.       rounded_addr += sizeof (int);
  1285.     }
  1286.     } 
  1287.   else 
  1288.     {
  1289.       if (lseek (corechan, uaddr, 0) < 0)
  1290.     perror_with_name ("seek on core file");
  1291.       if (myread (corechan, buf, sizeof (struct fpstate)) < 0) 
  1292.     perror_with_name ("read from core file");
  1293.       skip = 0;
  1294.     }
  1295.   
  1296.   fpstatep = (struct fpstate *)(buf + skip);
  1297.   print_387_status (fpstatep->status, (struct env387 *)fpstatep->state);
  1298. }
  1299.  
  1300. @
  1301.